import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/home/home.vue'
import login from '../views/login/login.vue'
import productList from '../views/productList/productList.vue'
import productDetail from '../views/productDetail/productDetail.vue'

import store from "@/store/index.js";

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
        index: 1
    }
}, {
    path: "/category",
    name: "Category",
    component: () =>
        import ("../views/category/category.vue"),
    meta: {
        index: 1
    }
}, {
    path: "/shopcart",
    name: "Shopcart",
    component: () =>
        import ("../views/shopcart/shopcart.vue"),
    meta: {
        index: 1,
        auth: true
    }
}, {
    path: "/user",
    name: "User",
    component: () =>
        import ("../views/user/user.vue"),
    meta: {
        index: 1,
        auth: true
    }
}, {
    path: '/login',
    component: login,
    meta: {
        index: 2
    }
}, {
    path: '/productList',
    component: productList,
    meta: {
        index: 2
    }
}, {
    path: '/product/:id',
    component: productDetail
}]

const router = new VueRouter({
    routes
})

router.beforeEach((to,from,next) => {
    // console.log('to:', to);
//   console.log('from:', from);
    if(to.meta.auth){
        console.log(store.getters);
        if(store.getters.getIsLogin){
            next()
        } else {
            next({ path: '/login' })
        }
    } else {
        next()
    }
})
export default router